<?php
declare (strict_types = 1);

namespace app\mobile\controller;

use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;
class User extends \app\mobile\controller\Auth
{
  public function userInfo()
  {
    $auth = getSettingCode('mini_auth');
    $this->user['mini_auth'] = $auth;
    // print_r($this->user);
    // return;
    return $this->doSuccess('ok',$this->user);
  }

  //可提现余额
  public function userDeposit()
  {
    $union = getSettingType('会员等级');
    $min_deposit = $union['min_deposit_fee'];
    $this->user['min_deposit'] = $min_deposit;
    $this->user['free_bonus'] = getDeposit($this->uid);
    return $this->doSuccess('ok',$this->user);
  }
  public function userAcc()
  {
    
    $account = Db::name('user_account')
              ->field('account,type,name')->where('uid',$this->uid)->find();
    if (!$account) {
      Db::name('user_account')->insert([
        'uid'=>$this->uid
      ]);
      $account = Db::name('user_account')
                      ->field('account,type,name')->where('uid',$this->uid)->find();
    }
    $this->user['account'] = $account;
    return $this->doSuccess('ok',$this->user);
  }
  
  public function me()
  {
    //累计提现
    $total_deposit = Db::name('deposit')
                ->where('uid',$this->uid)
                ->where('status','>',0)
                ->sum('fee');
    $r['total_deposit'] = $total_deposit;

    //待结算订单
    $order_settle = Db::name('order')
                ->where('uid',$this->uid)
                ->where('status','in','0,1,2')
                ->count('id');
    $r['order_settle'] = $order_settle;

    //待结算佣金
    $bonus_settle = Db::name('reward')
                ->where('to_uid',$this->uid)
                ->where('order_status',3)
                ->sum('fee');
    $r['bonus_settle'] = $bonus_settle;

    //账户余额
    $free_bonus = getDeposit($this->uid);
    $r['free_bonus'] = $free_bonus;
    return $this->doSuccess('ok',$r);
  }

  public function myRecommender()
  {
    $puser =Db::name('user')
              ->field('id,nick,head,type,wechat')
              ->where('id',$this->user['pid1'])
              ->find();
    return $this->doSuccess('ok',$puser);
  }

  //提现记录
  public function mydeposit()
  {
    $query = [];
    $source = input('source','pdd');
    $pageIndex = input('pageIndex');
    $limit = input('limit');
    $status = input('status','-2');
    if ($status==1) {
    $query[] = ['status','=',1];
      $query->where('status',1);
    }elseif ($status ===0) {
      $query[] = ['status','=',0];
    }
    $query[] = ['uid','=',$this->uid];
    $rows = Db::name('deposit')->where($query)->page($pageIndex,10)->select()->toArray();
    foreach ($rows as &$k) {
      
      $u = Db::name('user')->field('id,nick,head')->where('id',$k['uid'])->find();
      $k['user'] = $u;
      if ($k['status'] == 1) {
        $k['status_txt'] = '审核通过';
      }elseif ($k['status'] == -1) {
        $k['status_txt'] = $k['error'];
      }
      else{
        $k['status_txt'] = '审核中';
      }
    }
    $r['rows'] = $rows;
    return $this->doSuccess('ok',$r);
  }

  
  //创建海报
  public function createPoster()
  {
    $vo = input('post.');
    // $vo['my'] = $vo['url'];
    $bg_pic = $vo['url'];
    $pic_arr = explode("upload",$bg_pic);
    // $bg_pic = '.'.str_replace(Request::domain(),'',$bg_pic);
    $bg_pic = './upload'.$pic_arr[1];
    // print_r($pic_arr);exit();

    //小程序码
    $Qrcode = new \WeMini\Qrcode(minicfg());
    $page = 'views/index/index';
    $scene = $this->uid;
    $qrCodeData = $Qrcode->createMiniScene($scene,$page,430,false,["r" => "0", "g" => "0", "b" => "0"],false);

    // print_r($bg_pic);return;

    \app\common\Poster::setConfig([
      'bg_url' => $bg_pic,//背景图片路径
      'text'=>[
        [
          'text' => '邀请码:'.$this->uid,
          'left' => 400, //左侧字体开始的位置
          'top' => 1720, //字体的下边框
          'fontSize' => 32, //字号
          'width' => 400, 
          'fontColor' => '255,0,0', //字体颜色
          'angle' => 0,
        ]
      ],
      'image'=>[
        [
          'stream' => $qrCodeData, //图片资源是否是字符串图像流
          'left' => 300,
          'top' => 1220,
          'width' => 450,
          'height' => 450,
          // 'radius' => 50,
          // 'opacity' => 100
        ]
      ]
    ]);
    $qr_dir = './upload/'.$this->uid.'/poster/';
    $qr_fname = md5($this->uid.$bg_pic).'.png';
    $qr_filepath = $qr_dir.$qr_fname;
    if (!file_exists($qr_dir)) {
      mkdir($qr_dir,0777,true);
    }
    $r = \app\common\Poster::make($qr_filepath);

    $poster_url =  Request::domain().'/upload/'.$this->uid.'/poster/'.$qr_fname;
    
    $vo['my'] = $poster_url;
    return $this->doSuccess('ok',$vo);
    # code...
  }

  public function notice()
  {

    $list = [];
    // if ($this->sysbase['user_bind_phone']) {
    //   $v['title'] = '请绑定手机号';
    //   $v['url'] = '/views/user/profile';
    //   $v['opentype'] = 'navigate';
    //   $list[] = $v;
    // }
    $pos = input('pos');
    $list = Db::name('notice')
            // ->where('type','滚动')
            ->where('pos',$pos)
            ->select()->toArray();
    foreach ($list as &$k) {
      $k['opentype'] = 'navigate';
      $k['url'] = '/views/page/info?table=notice&id='.$k['id'];
    }
    $r['list'] = $list;
    $popup = Db::name('notice')
            ->where('type','弹窗')
            ->where('pos',$pos)
            ->find();
    $r['popup'] = $popup;
    return $this->doSuccess('ok',$r);
  }

  //提现操作
  public function doDeposit()
  {

    $code = input('code');
    $free_bonus = input('free_bonus');
    
    $alipay = input('alipay');
    if ($alipay) {
      return $this->doError('请输入提现支付宝帐号');
    }
    $mobile = input('mobile');
    $dofee = input('dofee');
    $name = input('name');
    if ($alipay) {
      return $this->doError('请输入提现支付宝真实姓名');
    }

    $day = date('d');
    $union = getSettingType('会员等级');
    $deposit_day = $union['deposit_day'];
    if ($day < $deposit_day) {
      return $this->doError($deposit_day.'号才可以提现');
    }
    $ck = Db::name('deposit')->where('ctime','today')->find();
    if ($ck) {
      return $this->doSuccess('ok','每天只能申请提现一次');
    }

    $min_deposit = $union['min_deposit_fee'];
    $free_bonus = getDeposit($this->uid);
    if ($dofee > $free_bonus) {
      return $this->doError('不能大于可提现余额');
    }elseif ($dofee < $min_deposit) {
      return $this->doError('提现金额不能小于'.$min_deposit);
    }
    $mobile = trim($mobile);
    $scode = Cache::get($mobile.'_code');
    if (trim($scode) != trim($code)) {
      return $this->doError('验证码错误');
    }
    $vo = array();
    $vo['account'] = $alipay;
    if ($name) {
      $vo['name'] = $name;
    }
    $vo['fee'] = $dofee;
    $vo['mobile'] = $mobile;
    $vo['uid'] = $this->uid;
    $vo['ctime'] = time();
    $vo['status'] = 0;
    $vo['type'] = '提现';
    $vo['account_type'] = '支付宝';
    Db::name('deposit')->insert($vo);
    return $this->doSuccess('提现申请已经提交，请等的审核');
  }

  //海报模板
  public function posterTpl()
  {
    $ads = Db::name('ads')
                ->field('pic as url,title')
                ->where('group','推广海报')
                ->order('id asc')
                ->select();
    foreach ($ads as $k) {
      $k['my'] = '';
    }
    return $this->doSuccess('ok',$ads);
    // print_r($ads);exit();
    $list = [
      [
        'url'=>Request::domain().'/upload/post1.jpg',
        'id'=>1,
        'title'=>'',
        'my'=>'',
      ],
      [
        'url'=>Request::domain().'/upload/post2.jpg',
        'id'=>2,
        'title'=>'',
        'my'=>'',
      ],
      [
        'url'=>Request::domain().'/upload/post3.jpg',
        'id'=>3,
        'title'=>'',
        'my'=>'',
      ],
      [
        'url'=>Request::domain().'/upload/post4.jpg',
        'id'=>4,
        'title'=>'',
        'my'=>'',
      ],
      [
        'url'=>'https://rebate.geemy.net/upload/post5.jpg',
        'id'=>5,
        'title'=>'',
        'my'=>'',
      ],
      
    ];
    return $this->doSuccess('ok',$list);
    # code...
  }

  public function getMobileCode()
  {
    $code = rand(1111,9999);
    return $this->doSuccess('ok',$code);
  }


  public function doProfile()
  {
    $account = input('post.account');
    Db::name('user')->where('id',$this->uid)->update([
      'nick'=>input('nick'),
      'alipay'=>input('alipay'),
      'name'=>input('name'),
    ]);

    // $ck = Db::name('user_account')->where('uid',$this->uid)->find();
    // if ($ck) {
    //   $account['status'] = 1;
    //   Db::name('user_account')->where('uid',$this->uid)->update($account);
    // }else{
    //   $account['uid'] = $this->uid;
    //   $account['status'] = 1;
    //   Db::name('user_account')->insert($account);
    // }
    return $this->doSuccess('ok');
  }

}